package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.domain.Order;
import com.ruoyi.module.mapper.OrderMapper;
import com.ruoyi.module.po.PointsDetailsPo;
import com.ruoyi.module.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.PointsMapper;
import com.ruoyi.module.domain.Points;
import com.ruoyi.module.service.IPointsService;

/**
 * 积分Service业务层处理
 *
 * @author fubojin
 * @date 2025-03-03
 */
@Service
public class PointsServiceImpl implements IPointsService {
    @Autowired
    private PointsMapper pointsMapper;
    @Autowired
    private IOrderService orderService;

    /**
     * 查询积分
     *
     * @param id 积分主键
     * @return 积分
     */
    @Override
    public Points selectPointsById(Long id) {
        Points points = pointsMapper.selectPointsById(id);
        setOrder(points);
        return points;
    }


    /**
     * 封装商品信息
     * @param points 积分
     */
    void setOrder(Points points) {
        if (points.getType() == 5 && points.getOrderId() != null){
            points.setOrder(orderService.selectOrderById(points.getOrderId()));
        }
    }
    /**
     * 查询积分列表
     *
     * @param points 积分
     * @return 积分
     */
    @Override
    public List<Points> selectPointsList(Points points) {
        List<Points> pointsList = pointsMapper.selectPointsList(points);
        for (Points points1 : pointsList) {
            setOrder(points1);
        }
        return pointsList;
    }

    /**
     * 新增积分
     *
     * @param points 积分
     * @return 结果
     */
    @Override
    public int insertPoints(Points points) {
        points.setCreateTime(DateUtils.getNowDate());
        return pointsMapper.insertPoints(points);
    }

    /**
     * 修改积分
     *
     * @param points 积分
     * @return 结果
     */
    @Override
    public int updatePoints(Points points) {
        points.setUpdateTime(DateUtils.getNowDate());
        return pointsMapper.updatePoints(points);
    }

    /**
     * 批量删除积分
     *
     * @param ids 需要删除的积分主键
     * @return 结果
     */
    @Override
    public int deletePointsByIds(Long[] ids) {
        return pointsMapper.deletePointsByIds(ids);
    }

    /**
     * 删除积分信息
     *
     * @param id 积分主键
     * @return 结果
     */
    @Override
    public int deletePointsById(Long id) {
        return pointsMapper.deletePointsById(id);
    }

    @Override
    public PointsDetailsPo getPointsDetails(Long userId) {
        PointsDetailsPo pointsDetails = pointsMapper.getPointsDetails(userId);
        //排名
        List<Points> pointsRanking = pointsMapper.getPointsRanking();
        // 遍历列表，找到目标 userId 的索引
        int j = 0;
        for (int i = 0; i < pointsRanking.size(); i++) {
            Points points = pointsRanking.get(i);
            if (userId.equals(points.getUserId())) {
                j = i + 1;
            }
        }
        pointsDetails.setPointsRanking(j);

        return pointsDetails;
    }
}
